<?php
namespace App\Console\Commands\Elasticsearch\Indices;
use Illuminate\Support\Facades\Artisan;
class OrderIndex
{
    public static function getAliasName()
    {
        return 'orders';
    }

    public static function getProperties()
    {
        return [
            'id'=>['type'=>'integer'],
            'order_no'=>['type'=>'keyword'],
            'order_amount'=>['type'=>'scaled_float','scaling_factor'=>100],
            'order_status'=>['type'=>'byte'],
            'created_at'=>['type'=>'keyword'],
            'user_name'=>['type'=>'text',"analyzer"=>"ik_max_word", "search_analyzer"=>"ik_smart_synonym"],
            'consignee'=>['type'=>'text',"analyzer"=>"ik_max_word", "search_analyzer"=>"ik_smart_synonym"],
            'store_name'=>['type'=>'text',"analyzer"=>"ik_max_word", "search_analyzer"=>"ik_smart_synonym"],
            'addresses'=>['type'=>'text',"analyzer"=>"ik_max_word", "search_analyzer"=>"ik_smart_synonym"],
            'goods'=>[
                'type'=>'nested',
                'properties'=>[
                    'goods_name'=>['type'=>'text',"analyzer"=>"ik_max_word", "search_analyzer"=>"ik_smart_synonym",'copy_to'=>'goodsName'],
                    'goods_spec'=>['type'=>'text',"analyzer"=>"ik_max_word", "search_analyzer"=>"ik_smart_synonym",'copy_to'=>'goodsSpec'],
                    'goods_price'=>['type'=>'scaled_float','scaling_factor'=>100]
                ]
            ],
            'properties'=>[
                'type'=>'nested',
                'properties'=>[
                    'name'=>['type'=>'keyword','copy_to'=>'properties_name'],
                    'value'=>['type'=>'keyword','copy_to'=>'properties_value'],
                    'search_value'=>['type'=>'keyword']
                ]
            ]
        ];
    }

    public static function getSettings()
    {
        return [
            'number_of_replicas' => 0,
          //  'refresh_interval' => - 1,//-1为不刷新索引
            'analysis' => [
                'analyzer' => [
                    'ik_smart_synonym' => [
                        'type'      => 'custom',
                        'tokenizer' => 'ik_max_word',
                        'filter'    => ['synonym_filter'],
                    ],
                ],
                'filter'   => [
                    'synonym_filter' => [
                        'type'          => 'synonym',
                        'synonyms_path' => 'analysis/synonyms.txt',
                    ],
                ],
            ],
        ];
    }

    public static function rebuild($indexName)
    {
        Artisan::call('es:sync-data', ['index' => $indexName]);
    }
}
